package com.gray.service.impl;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gray.common.Page;
import com.gray.common.UploadImage;
import com.gray.dao.Stay_MessageMapper;
import com.gray.dao.UserMapper;
import com.gray.dto.StayMessage;
import com.gray.pojo.Stay_Message;
import com.gray.pojo.User;
import com.gray.service.IUserService;
import com.gray.util.BasePathUtil;
import com.gray.util.CookieUtil;
import com.gray.util.ImageUtil;


@Service
public class UserService implements IUserService {

	@Autowired
	private UserMapper user;
	@Autowired
	private Stay_MessageMapper stayMessage;

	@Override
	public User getUserById(int id) {

		return this.user.selectByPrimaryKey(id);
	}

	/*
	 * 用户登录 (non-Javadoc)
	 * 
	 * @see com.gray.service.IUserService#Login(java.lang.String,
	 * java.lang.String)
	 */

	@Override
	public int Login(String username, String password) {
		// TODO Auto-generated method stub

		User user = this.user.selectByUsernameAndPassword(username, password);

		if (user != null) {// 成功查询到用户
			return 200;
		}

		return 500;
	}

	@Override
	public User getUserByNameAndPwd(String username, String password) {
		// TODO Auto-generated method stub
		User user = this.user.selectByUsernameAndPassword(username, password);
		return user;
	}

	/**
	 * 用户注册
	 * 
	 */
	
	@Transactional
	@Override
	public int register(String username, String password, String email,String userRegisterIp) {
		User user = this.user.selectByUsername(username);
		if (user == null) {// 没有查询到该用户名
			user = new User();
			user.setUserName(username);
			user.setUserPwd(password);
			user.setUserEmail(email);
			System.out.println(user.getUserEmail());
			int i = this.user.insertSelective(user);
			if(i>0){
			return user.getUserId();}else{
			return -1;
			}
		}

		return -1;
	}

	/*
	 * (获取留言列表)
	 * 
	 * @see com.gray.service.IUserService#getStayMessage(int)
	 */

	@Override
	public List<StayMessage> getStayMessage(int userId) {

		return this.stayMessage.selectStayMessageByUserId(userId);
	}

	@Override
	public int reportStayMessage(Integer user_id, Integer stayUserId, String message_content, String stayUserIp) {
		Stay_Message stay_Message = new Stay_Message();
		stay_Message.setUserId(user_id);
		stay_Message.setStayUserId(stayUserId);
		stay_Message.setMessageContent(message_content);
		stay_Message.setStayUserIp(stayUserIp);
		;
		return this.stayMessage.insertSelective(stay_Message);
	}
	
	@Override
	public Map getStayMessageByPage(Short userId, Integer currentPage, Integer everyPage) {
		Integer totalNum = this.stayMessage.selectTotalCountById(userId); //获取总记录数
		Page page = new Page(totalNum, currentPage, everyPage); 
		List list = this.stayMessage.selectStayMessageByPage(userId, page.getDbFromIndex(), everyPage);
		Map result = new HashMap();
		result.put("page", page);
		result.put("list", list);

		return result;
	}

	@Override
	@Transactional
	public int updataUser(User user, UploadImage uploadimage) {
		String basePath = BasePathUtil.getImgBasePath();
//		String tempPath = "E:/file/image/";
//		if(!System.getProperty("os.name").equals("Windows 7")){
//			tempPath = "/root/file/image";
//		}
		
		if (uploadimage.getOriginalFilename() != null && uploadimage.getInputStream() != null) {
			//保存图片
			String ImageUrl = ImageUtil.imageAdd(uploadimage.getInputStream(), "touxiang"+ System.getProperty("file.separator")+user.getUserId().toString()+user.getUserName(), uploadimage.getOriginalFilename());
			try {
				//图片缩列
				ImageUtil.thumBnails(436, basePath+ImageUrl);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//删除原来的图片
			if(user.getUserImageUrl()!=null){
				
				ImageUtil.imageDel(user.getUserImageUrl());
			}
			user.setUserImageUrl(ImageUrl);
		}
		return this.user.updateByPrimaryKeySelective(user);
	}
	
	@Override
	public int updataUser(User user) {
		return this.user.updateByPrimaryKey(user);
	}

	@Override
	public int batchDeleteStayMessage(List list) {
		
		return stayMessage.batchDeleteStayMessage(list);
	}

}
